<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用车申请编号" prop="requestId">
        <el-input
          v-model="queryParams.requestId"
          placeholder="请输入用车申请编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆编号" prop="carId">
        <el-input
          v-model="queryParams.carId"
          placeholder="请输入车辆编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="驾驶员编号" prop="driverId">
        <el-input
          v-model="queryParams.driverId"
          placeholder="请输入驾驶员编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划调度开始时间" prop="scheduledStartTime">
        <el-date-picker clearable
          v-model="queryParams.scheduledStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择计划调度开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计划调度结束时间" prop="scheduledEndTime">
        <el-date-picker clearable
          v-model="queryParams.scheduledEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择计划调度结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实际调度开始时间" prop="actualStartTime">
        <el-date-picker clearable
          v-model="queryParams.actualStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择实际调度开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实际调度结束时间" prop="actualEndTime">
        <el-date-picker clearable
          v-model="queryParams.actualEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择实际调度结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="调度编号" align="center" prop="scheduleId" / v-if="false">
      <el-table-column label="用车申请编号" align="center" prop="requestId" />

      <el-table-column label="车辆类型" align="center" prop="model" />

      <el-table-column label="驾驶员姓名" align="center" prop="name" />
      <el-table-column label="计划调度开始时间" align="center" prop="scheduledStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scheduledStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划调度结束时间" align="center" prop="scheduledEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scheduledEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际调度开始时间" align="center" prop="actualStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actualStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际调度结束时间" align="center" prop="actualEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actualEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>


      <el-table-column prop="status" label="调度状态(1进行中，2已完成，3已取消)" width="100">
        <template slot-scope="scope">
        <dict-tag :options="dict.type.lh_management_status" :value="scope.row.status"/>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['lh:management:edit']"
          >修改调度时间</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['lh:management:remove']"
          >删除</el-button>



          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="complete(scope.row)"
            v-hasPermi="['lh:management:complete']"
             v-show="scope.row.status==1|scope.row.status==3" 
          >完成调度</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆调度管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用车申请编号" prop="requestId">
          <el-input v-model="form.requestId" placeholder="请输入用车申请编号" />
        </el-form-item>
        <el-form-item label="车辆编号" prop="carId">
          <el-input v-model="form.carId" placeholder="请输入车辆编号" />
        </el-form-item>
        <el-form-item label="驾驶员编号" prop="driverId">
          <el-input v-model="form.driverId" placeholder="请输入驾驶员编号" />
        </el-form-item>
        <el-form-item label="实际调度开始时间" prop="actualStartTime">
          <el-date-picker clearable
            v-model="form.actualStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择实际调度开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际调度结束时间" prop="actualEndTime">
          <el-date-picker clearable
            v-model="form.actualEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择实际调度结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listManagement, getManagement, delManagement, addManagement, updateManagement, completeManagement} from "@/api/lh/management";

export default {
  name: "Management",
  dicts:['lh_management_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆调度管理表格数据
      managementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        requestId: null,
        carId: null,
        driverId: null,
        scheduledStartTime: null,
        scheduledEndTime: null,
        actualStartTime: null,
        actualEndTime: null,
        status: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        requestId: [
          { required: true, message: "用车申请编号", trigger: "blur" }
        ],
        carId: [
          { required: true, message: "车辆编号", trigger: "blur" }
        ],
        driverId: [
          { required: true, message: "驾驶员编号", trigger: "blur" }
        ],
        actualStartTime: [
          { required: true, message: "计划调度开始时间不能为空", trigger: "blur" }
        ],
        actualEndTime: [
          { required: true, message: "计划调度结束时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车辆调度管理列表 */
    getList() {
      this.loading = true;
      listManagement(this.queryParams).then(response => {
        this.managementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scheduleId: null,
        requestId: null,
        carId: null,
        driverId: null,
        scheduledStartTime: null,
        scheduledEndTime: null,
        actualStartTime: null,
        actualEndTime: null,
        status: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scheduleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆调度管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scheduleId = row.scheduleId || this.ids
      getManagement(scheduleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆调度管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.scheduleId != null) {
            updateManagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scheduleIds = row.scheduleId || this.ids;
      this.$modal.confirm('是否确认删除车辆调度管理编号为"' + scheduleIds + '"的数据项？').then(function() {
        return delManagement(scheduleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lh/management/export', {
        ...this.queryParams
      }, `management_${new Date().getTime()}.xlsx`)
    },
  /** 调度完成按钮操作 */
complete(row) {
  this.reset();  // 重置表单
  const scheduleId = row.scheduleId || this.ids;
  
  // 调用完成调度的 API
  completeManagement(scheduleId).then(response => {
    // 更新列表数据
    this.getList();  // 获取最新的调度管理列表
    this.$modal.msgSuccess("调度完成");

    // 进行一些UI更新操作
    this.title = "完成车辆调度管理";
    this.open = false;  // 关闭弹出层
  }).catch(error => {
    this.$modal.msgError("完成调度失败");
  });
},



  }
};
</script>
